package com.saer.client.form.admin.permission;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.saer.client.button.admin.permissions.ClearButtonItem;
import com.saer.client.button.admin.permissions.SubmitButtonItem;
import com.saer.client.common.Utils;
import com.saer.client.dto.UserInfoDto;
import com.saer.client.grid.admin.permissions.UserDetailsGrid;
import com.saer.client.text.admin.permissions.FirstName;
import com.saer.client.text.admin.permissions.LastName;
import com.saer.client.text.admin.permissions.UserId;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class PermissionsSearchForm extends DynamicForm {

	private UserId id = new UserId();
	private FirstName firstName = new FirstName();
	private LastName lastName = new LastName();
	private SubmitButtonItem submitButton = new SubmitButtonItem();
	private ClearButtonItem clearButton = new ClearButtonItem();
	
	private UserDetailsGrid userDetails;

	public PermissionsSearchForm(UserDetailsGrid userDetailsGrid) {
		
		this.userDetails = userDetailsGrid;
		
		this.setSize("50%", "100%");
		this.setIsGroup(true);
		this.setGroupTitle("User Search");
		this.setTitleOrientation(TitleOrientation.TOP);
		this.setNumCols(4);
		this.setColWidths("85", "85", "*", "*");
		this.setPadding(10);
		
		this.submitButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clearGrids();
//				Cameogwt.getRemotewebservice().getUserInfo(
//						id.getValueAsString(),
//						firstName.getValueAsString(),
//						lastName.getValueAsString(),
//						new GetUserInfoCallBack());
			}
		});
		
		this.clearButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				id.clearValue();
				firstName.clearValue();
				lastName.clearValue();
			}
		});
		
		SpacerItem spacer = new SpacerItem();
		spacer.setColSpan(4);
		
		this.setFields(id, firstName, lastName, spacer, submitButton, clearButton);
	}
	
	private void clearGrids() {
		userDetails.clearData();
		userDetails.getUserPermissions().clearData();
		userDetails.getAllPermissions().clearData();
	}
	
	private class GetUserInfoCallBack implements AsyncCallback<List<UserInfoDto>> {
		
		public void onSuccess(List<UserInfoDto> result) {
			if (!Utils.isEmpty(result)) {
				userDetails.setData(result);
				if (result.size() == 1) {
					String userId = result.get(0).getUserId();
					userDetails.getUserPermissions().loadPermissions(userId);
					userDetails.getAllPermissions().loadPermissions(userId);
				}
			} else {
				SC.warn("No user found.");
			}
		}

		public void onFailure(Throwable caught) {

		}
	}

}
